<?php

/**
 * api actions.
 *
 * @package    travis
 * @subpackage api
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class apiActions extends sfActions {

    public function executeLogin(sfWebRequest $request) {
        $email = $request->getPostParameter("email");
        $password = $request->getPostParameter("password");

        if (isset($email) && isset($password)) {
            $result = sbUser::userLogin($email, $password);
            if ($result) {
                $data = $result->getEmail() . "/" . $result->getPermissions() . "/" . $result->getId() . "/" . time();
                $this->getResponse()->setCookie("troli", base64_encode($data));
                $result->setUpdateTime(sbValues::getCurrentTime());
                $result->save();
                die(json_encode(array("status" => "ok", "user" => $result->toArray())));
            } else {
                die(json_encode(array("status" => "error", "user" => "E-mail or password incorrect.")));
            }
        } else {
            die(json_encode(array("status" => "error", "user" => "E-mail or password incorrect.")));
        }
    }

    public function executeResetPassword(sfWebRequest $request) {
        $username = $request->getPostParameter("username");

        if (isset($username)) {
            $result = sbUser::resetPassword($username);
            if ($result == sbValues::PASSWORT_RESET_SUCCSESS) {
                die(json_encode(array('status' => 'success', 'message' => 'At this address, we sent an email with instructions.')));
            } else {
                die(json_encode(array('status' => 'error', 'message' => 'In our database, there is no user with the address ' . $username . '.')));
            }
        } else {
            die(json_encode(array('status' => 'error', 'message' => 'username is empty or incorrect')));
        }
    }

    public function executeSignUp(sfWebRequest $request) {
        $first_name = $request->getPostParameter("first_name");
        $last_name = $request->getPostParameter("last_name");
        $email = $request->getPostParameter("email");
        $password = $request->getPostParameter("password");
        $phone = $request->getPostParameter("phone");
        $country = $request->getPostParameter("country");
        $agent = $request->getPostParameter("agent");
        $language = $request->getPostParameter("language");
        /*HACK*/
        $language=1;
        if (isset($first_name) && isset($last_name) && isset($email) && isset($password) && isset($phone) && isset($country) && isset($agent) && isset($language)) {
            $result = sbUser::createNewUser($first_name, $last_name, $email, $phone, $password, $country, $agent, $language);
            die(json_encode(array('status' => 'success', 'message' => $result)));
        } else {
            die(json_encode(array('status' => 'error', 'message' => 'incorect values')));
        }
    }

    public function executeAddCountry(sfWebRequest $request) {
        $en = $request->getPostParameter('en');
        $ru = $request->getPostParameter('ru');
        $he = $request->getPostParameter('he');

        if (isset($ru) && isset($en) && isset($he)) {
            sbData::addCountry($en, $he, $ru);
            die();
        } else {
            die();
        }
    }

    public function executeEditCountry(sfWebRequest $request) {
        $id = $request->getPostParameter('id');
        $en = $request->getPostParameter('en');
        $ru = $request->getPostParameter('ru');
        $he = $request->getPostParameter('he');

        if (isset($ru) && isset($en) && isset($he) && isset($id)) {
            sbData::editCountry($en, $he, $ru, $id);
            die();
        } else {
            die();
        }
    }

    public function executeAddRegion(sfWebRequest $request) {
        $country = $request->getPostParameter('country');
        $en = $request->getPostParameter('en');
        $ru = $request->getPostParameter('ru');
        $he = $request->getPostParameter('he');

        if (isset($ru) && isset($en) && isset($he) && isset($country)) {
            sbData::addRegion($en, $he, $ru, $country);
            die();
        } else {
            die();
        }
    }

    public function executeEditRegion(sfWebRequest $request) {
        $id = $request->getPostParameter('id');
        $country = $request->getPostParameter('country');
        $en = $request->getPostParameter('en');
        $ru = $request->getPostParameter('ru');
        $he = $request->getPostParameter('he');

        if (isset($ru) && isset($en) && isset($he) && isset($country) && isset($id)) {
            sbData::editRegion($en, $he, $ru, $country, $id);
            die();
        } else {
            die();
        }
    }

    public function executeAddCity(sfWebRequest $request) {
        $country = $request->getPostParameter('country');
        $region = $request->getPostParameter('region');
        $priority = $request->getPostParameter('priority');
        $en = $request->getPostParameter('en');
        $ru = $request->getPostParameter('ru');
        $he = $request->getPostParameter('he');

        if (isset($ru) && isset($en) && isset($he) && isset($country) && isset($priority)) {
            sbData::addCity($en, $he, $ru, $country, $region, $priority);
            die();
        } else {
            die();
        }
    }

    public function executeEditCity(sfWebRequest $request) {
        $id = $request->getPostParameter('id');
        $country = $request->getPostParameter('country');
        $region = $request->getPostParameter('region');
        $priority = $request->getPostParameter('priority');
        $en = $request->getPostParameter('en');
        $ru = $request->getPostParameter('ru');
        $he = $request->getPostParameter('he');

        if (isset($ru) && isset($en) && isset($he) && isset($country) && isset($priority) && isset($id)) {
            sbData::editCity($en, $he, $ru, $country, $region, $priority, $id);
            die();
        } else {
            die();
        }
    }

    public function executeAddProvider(sfWebRequest $request) {
        $name = $request->getPostParameter('name');
        $short = $request->getPostParameter('short');
        $username = $request->getPostParameter('username');
        $pass = $request->getPostParameter('pass');

        if (isset($name) && isset($short)) {
            sbData::addProvider($name, $short, $username, $pass);
            die();
        } else {
            die();
        }
    }

    public function executeEditProvider(sfWebRequest $request) {
        $id = $request->getPostParameter('id');
        $name = $request->getPostParameter('name');
        $short = $request->getPostParameter('short');
        $username = $request->getPostParameter('username');
        $pass = $request->getPostParameter('pass');

        if (isset($name) && isset($short) && isset($id)) {
            sbData::editProvider($name, $short, $username, $pass, $id);
            die();
        } else {
            die();
        }
    }

    public function executeAddUser(sfWebRequest $request) {
        $title = $request->getPostParameter('title');
        $first_name = $request->getPostParameter('first_name');
        $last_name = $request->getPostParameter('last_name');
        $email = $request->getPostParameter('email');
        $password = $request->getPostParameter('password');
        $phone1 = $request->getPostParameter('phone1');
        $phone2 = $request->getPostParameter('phone2');
        $fax = $request->getPostParameter('fax');
        $country = $request->getPostParameter('country');
        $city = $request->getPostParameter('city');
        $address = $request->getPostParameter('address');
        $permissions = $request->getPostParameter('permissions');
        $agent = $request->getPostParameter('agent');
        $status = $request->getPostParameter('status');
        $commissions = $request->getPostParameter('commissions');
        $latitude=$request->getPostParameter('lat');
        $logitude=$request->getPostParameter('lng');        
        
        if (isset($first_name) && isset($last_name) && isset($email) && isset($password) && isset($phone1) && isset($country) && isset($city) && isset($address) && isset($permissions) && isset($status)) {
            $result = sbData::addUser($title, $first_name, $last_name, $email, $password, $phone1, $phone2, $fax, $country, $city, $address, $permissions, $agent, $status, $commissions,$latitude,$logitude);
            die($result);
        } else {
            die();
        }
    }

    public function executeEditUser(sfWebRequest $request) {
        $id = $request->getPostParameter('id');
        $title = $request->getPostParameter('title');
        $first_name = $request->getPostParameter('first_name');
        $last_name = $request->getPostParameter('last_name');
        $email = $request->getPostParameter('email');
        $password = $request->getPostParameter('password');
        $phone1 = $request->getPostParameter('phone1');
        $phone2 = $request->getPostParameter('phone2');
        $fax = $request->getPostParameter('fax');
        $country = $request->getPostParameter('country');
        $city = $request->getPostParameter('city');
        $address = $request->getPostParameter('address');
        $permissions = $request->getPostParameter('permissions');
        $agent = $request->getPostParameter('agent');
        $status = $request->getPostParameter('status');
        $commissions = $request->getPostParameter('commissions');
        $latitude=$request->getPostParameter('lat');
        $logitude=$request->getPostParameter('lng');
                
        if (isset($first_name) && isset($last_name) && isset($email) && isset($password) && isset($phone1) && isset($country) && isset($city) && isset($address) && isset($permissions) && isset($status) && isset($id)) {
            #$result = sbData::editUser($id, $title, $first_name, $last_name, $email, $password, $phone1, $phone2, $fax, $country, $city, $address, $permissions, $agent, $status, $commissions);
              $result = sbData::editUser($id, $title, $first_name, $last_name, $email, $password, $phone1, $phone2, $fax, $country, $city, $address, $permissions, $agent, $status, $commissions,$latitude,$logitude);
            die($result);
        } else {
            die();
        }
    }

    public function executeDeactivateUser(sfWebRequest $request) {
        $id = $request->getPostParameter('id');

        if (isset($id)) {
            sbData::deactivateUser($id);
            die();
        } else {
            die();
        }
    }

    public function executeSearchAdminHotel(sfWebRequest $request) {
        $city_id = $request->getPostParameter('city');
        $hotel_id = $request->getPostParameter('hotel');
        $provider = $request->getPostParameter('provider');
        $page = $request->getPostParameter('page');
        $stars = $request->getPostParameter('stars');

        $result = sbHotels::serachHotelsPanel($city_id, $hotel_id, $provider, $stars, $page);

        die(json_encode($result));
    }

    public function executeAddHotel(sfWebRequest $request) {
        $name_en = $request->getPostParameter('name_en');
        $name_ru = $request->getPostParameter('name_ru');
        $name_he = $request->getPostParameter('name_he');
        $stars = $request->getPostParameter('stars');
        $country = $request->getPostParameter('country');
        $region = $request->getPostParameter('region');
        $city = $request->getPostParameter('city');
        $address = $request->getPostParameter('address');
        $phone1 = $request->getPostParameter('phone1');
        $phone2 = $request->getPostParameter('phone2');
        $fax = $request->getPostParameter('fax');
        $email = $request->getPostParameter('email');
        $description_en = $request->getPostParameter('description_en');
        $description_ru = $request->getPostParameter('description_ru');
        $description_he = $request->getPostParameter('description_he');
        $priority = $request->getPostParameter('priority');
        $images = $request->getPostParameter('images');
        $provider = $request->getPostParameter('provider');
        $web = $request->getPostParameter('web');

        if (isset($name_en) && isset($name_ru) && isset($name_he) && isset($stars) && isset($country) && isset($region)) {
            $result = sbData::addHotel($name_en, $name_ru, $name_he, $stars, $country, $region, $city, $address, $phone1, $phone2, $fax, $email, $description_en, $description_ru, $description_he, $priority, $images, $provider, $web);
            die($result);
        } else {
            die(300);
        }
    }

    public function executeEditHotel(sfWebRequest $request) {
        $id = $request->getPostParameter('id');
        $name_en = $request->getPostParameter('name_en');
        $name_ru = $request->getPostParameter('name_ru');
        $name_he = $request->getPostParameter('name_he');
        $stars = $request->getPostParameter('stars');
        $country = $request->getPostParameter('country');
        $region = $request->getPostParameter('region');
        $city = $request->getPostParameter('city');
        $address = $request->getPostParameter('address');
        $phone1 = $request->getPostParameter('phone1');
        $phone2 = $request->getPostParameter('phone2');
        $fax = $request->getPostParameter('fax');
        $email = $request->getPostParameter('email');
        $description_en = $request->getPostParameter('description_en');
        $description_ru = $request->getPostParameter('description_ru');
        $description_he = $request->getPostParameter('description_he');
        $priority = $request->getPostParameter('priority');
        $images = $request->getPostParameter('images');
        $provider = $request->getPostParameter('provider');
        $web = $request->getPostParameter('web');

        if (isset($name_en) && isset($name_ru) && isset($name_he) && isset($stars) && isset($country) && isset($region) && isset($id)) {
            $result = sbData::editHotel($name_en, $name_ru, $name_he, $stars, $country, $region, $city, $address, $phone1, $phone2, $fax, $email, $description_en, $description_ru, $description_he, $priority, $images, $provider, $web, $id);
            die($result);
        } else {
            die(300);
        }
    }

    public function executeDeleteHotel(sfWebRequest $request) {
        $id = $request->getPostParameter('id');

        if (isset($id)) {
            sbData::deleteHotel($id);
            die();
        } else {
            die();
        }
    }

    public function executeAddPlan(sfWebRequest $request) {
        $en = $request->getPostParameter('name_en');
        $ru = $request->getPostParameter('name_ru');
        $he = $request->getPostParameter('name_he');

        if (isset($en)) {
            sbData::addPlan($en, $ru, $he);
            die();
        } else {
            die();
        }
    }

    public function executeEditPlan(sfWebRequest $request) {
        $id = $request->getPostParameter('id');
        $en = $request->getPostParameter('name_en');
        $ru = $request->getPostParameter('name_ru');
        $he = $request->getPostParameter('name_he');

        if (isset($en) && isset($id)) {
            sbData::editPlan($en, $ru, $he, $id);
            die();
        } else {
            die();
        }
    }

    public function executeAddRoom(sfWebRequest $request) {
        $hotel_id = $request->getPostParameter('hotel_id');
        $name = $request->getPostParameter('name');
        $code = $request->getPostParameter('code');
        $description_en = $request->getPostParameter('description_en');
        $description_ru = $request->getPostParameter('description_ru');
        $description_he = $request->getPostParameter('description_he');
        $adult = $request->getPostParameter('adult');
        $children = $request->getPostParameter('children');
        $baby = $request->getPostParameter('baby');

        if (isset($hotel_id) && isset($name)) {
            sbData::addRoom($hotel_id, $name, $description_en, $description_ru, $description_he, $adult, $children, $code, $baby);
            die();
        } else {
            die();
        }
    }

    public function executeEditRoom(sfWebRequest $request) {
        $id = $request->getPostParameter('id');
        $hotel_id = $request->getPostParameter('hotel_id');
        $name = $request->getPostParameter('name');
        $code = $request->getPostParameter('code');
        $description_en = $request->getPostParameter('description_en');
        $description_ru = $request->getPostParameter('description_ru');
        $description_he = $request->getPostParameter('description_he');
        $adult = $request->getPostParameter('adult');
        $children = $request->getPostParameter('children');
        $baby = $request->getPostParameter('baby');

        if (isset($hotel_id) && isset($name) && isset($id)) {
            sbData::editRoom($hotel_id, $name, $description_en, $description_ru, $description_he, $adult, $children, $id, $code, $baby);
            die();
        } else {
            die();
        }
    }

    public function executeAddPrice(sfWebRequest $request) {
        $hotel_id = $request->getPostParameter('hotel_id');
        $room_id = $request->getPostParameter('room_id');
        $type = $request->getPostParameter('type');
        $date = $request->getPostParameter('date');
        $period = $request->getPostParameter('period');
        $fit = $request->getPostParameter('fit');
        $cascade = $request->getPostParameter('cascade');
        $special = $request->getPostParameter('special');
        $commission = $request->getPostParameter('commission');
        $market = $request->getPostParameter('market');

        if (isset($hotel_id) && isset($room_id)) {
            sbData::addPrice($hotel_id, $room_id, $type, $date, $period, $fit, $cascade, $special, $commission, $market);
            die();
        } else {
            die();
        }
    }

    public function executeEditPrice(sfWebRequest $request) {
        $id = $request->getPostParameter('id');
        $hotel_id = $request->getPostParameter('hotel_id');
        $room_id = $request->getPostParameter('room_id');
        $type = $request->getPostParameter('type');
        $date = $request->getPostParameter('date');
        $period = $request->getPostParameter('period');
        $fit = $request->getPostParameter('fit');
        $cascade = $request->getPostParameter('cascade');
        $special = $request->getPostParameter('special');
        $commission = $request->getPostParameter('commission');
        $market = $request->getPostParameter('market');

        if (isset($id)) {
            sbData::editPrice($hotel_id, $room_id, $type, $date, $period, $fit, $cascade, $special, $id, $commission, $market);
            die();
        } else {
            die();
        }
    }

    public function executeUserOrders(sfWebRequest $request) {
        $user_id = $request->getPostParameter('id');
        $order_id = $request->getPostParameter('order_id');
        $from_date = $request->getPostParameter('from_date');
        $to_date = $request->getPostParameter('to_date');
        $page = $request->getPostParameter('page');

        if (isset($user_id)) {
            $result = sbUser::userOrders($user_id, $order_id, $from_date, $to_date, $page);
            die(json_encode($result));
        }
    }

    public function executeGetHotels(sfWebRequest $request) {
        $sort = $request->getPostParameter('sort');
        $page = $request->getPostParameter('page');
        $permissions = $request->getPostParameter('permissions');

        if (isset($sort) && isset($page)) {
            $result = sbHotels::getInfoHotels($sort, $page);
            die(json_encode($result));
        }
    }

    public function executeSearchHotels(sfWebRequest $request) {
        $city_id = $request->getPostParameter('city_id');
        $hotel_id = $request->getPostParameter('hotel_id');
        $date = $request->getPostParameter('date');
        $night = $request->getPostParameter('night');
        $stars = $request->getPostParameter('stars');
        $rooms = $request->getPostParameter('rooms');
        $civic = $request->getPostParameter('civic');
        $sort = $request->getPostParameter('sort');
        $page = $request->getPostParameter('page');

        $result = sbHotels::searchHotels($city_id, $hotel_id, $date, $night, $stars, $rooms, $civic, $sort, $page);
        if ($result) {
            die(json_encode($result));
        }
    }

    public function executeGetISR(sfWebRequest $request) {
        
//        $cron = new cronGoc();
//        $client = $cron->start();
//        die();
//        $client = $cron->connectToGoc();
//        $result = $cron->getAvalibleRoomsPerHotel($client['cause'], 71, '2014-04-30');
//        $xml = simplexml_load_string($result->Get_Available_RoomsResult);
//        
//        die(var_dump($xml));
        
//        $isr = new sbIsratel();
//        $client = $isr->connect(sbValues::CIVIC);
//        $search = $isr->search($client, 'PR', '2014-06-15', 1, 1);
//        var_dump($search);
//        die();
        
//        $dan = new sbDan();
//        $result = $dan->connect();
//        var_dump($result);
//        die();
        
        $goc = new sbGoc();
        $client = $goc->connect();
//        $result = $goc->getHotels($client['cause']);
//        $parse = $goc->parseGetHotels($result);
        $result = $goc->GetAvalibleRoomByCity($client['cause']);
        $parse = $goc->parseAvalRoom($result);
        $result = $goc->insertLocalData($parse);
                
        die(json_encode($result));
    }
    
    public function GetRepository($client) {
        return $client->GetRepository(array(
            'Options' => array(
                'RegionsOption' => "BaseInfo",
                'HotelsOption' => "CodesOnly",
                'RoomsOption' => "CodesOnly",
                'HotelFacilitiesOption' => "BaseInfo",
                'RoomFacilitiesOption' => 'BaseInfo',
                'BoardsOption' => 'BaseInfo'
            )
                )
        );
    }
}
